//
//  CPSETController.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPSETController.h"
#import "UIViewController+MMDrawerController.h"
#import "CPNavController.h"
@interface CPSETController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UITableView *setTabView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) CPTableViewHeader *header;
@end

@implementation CPSETController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *back = [[UIImageView alloc] initWithFrame:self.view.bounds];
    back.contentMode = UIViewContentModeScaleAspectFill;
    back.clipsToBounds = YES;
    back.image = [UIImage imageNamed:@"01"];
    [self.view addSubview:back];

    [self cp_loadData];
    [self cp_setuptabview];
}
- (void)cp_setuptabview
{
    CPModel *model = [[CPSQLTools cp_sharedInstance] cp_shareRandom:1 share:NO].firstObject;
    CPTableViewHeader *header = [[CPTableViewHeader alloc] initWithFrame:CGRectMake(0, 0, kW, kW*0.5)];
    header.hTit = @[[NSString stringWithFormat:@"%@-%@", model.cp_title ,model.cp_author], model.cp_content];
    [self.view addSubview:header];
    header.backgroundColor = [UIColor clearColor];
    UITableView *setTabView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(header.frame), kW, kH-CGRectGetMaxY(header.frame)) style:(UITableViewStylePlain)];
    [setTabView registerClass:[CPSETTableViewCell class] forCellReuseIdentifier:@"cell"];
    setTabView.delegate = self;
    setTabView.dataSource = self;
    setTabView.backgroundColor = [UIColor clearColor];
    setTabView.separatorColor = JMColorRGBA(230, 230, 230, 0.8);
    setTabView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:setTabView];
    self.setTabView = setTabView;
}

#pragma mark --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    CPSETTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {cell = [[CPSETTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];}
    cell.model = _dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CPModel *model = _dataSource[indexPath.row];
    Class class = NSClassFromString(model.vc);
    id target = [[class alloc] init];
    [target setTitle:model.title];
    CPNavController *nav = (CPNavController *)self.mm_drawerController.centerViewController;
    [nav pushViewController:target animated:YES];
    [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
}

- (void)cp_loadData
{
    NSArray *setA = @[
                      @{@"title":@"诗人简介", @"icon":@"profile_light",@"vc":@"CPAUTHORController"},
                      @{@"title":@"诗词分享", @"icon":@"share",@"vc":@"CPSHAREController"},
                      @{@"title":@"我的收藏", @"icon":@"favor",@"vc":@"CPSAVEController"},
                      @{@"title":@"系统推荐", @"icon":@"creative",@"vc":@"CPRECOMENDController"},
                      @{@"title":@"浏览历史", @"icon":@"time",@"vc":@"CPHISTORYController"},
                      @{@"title":@"使用反馈", @"icon":@"edit",@"vc":@"CPFEEDBACKController"},
                      @{@"title":@"关于我们", @"icon":@"info",@"vc":@"CPABOUTController"},
                      ];
    self.dataSource = [NSMutableArray array];
    for (NSDictionary *dic in setA) {
        CPModel *model = [[CPModel alloc] init];
        model.icon = dic[@"icon"];
        model.title = dic[@"title"];
        model.vc = dic[@"vc"];
        [_dataSource addObject:model];
    }
}

@end

#pragma mark - **************** CPTableViewHeader
@implementation CPTableViewHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
//        UIImageView *bkIma = [[UIImageView alloc] initWithFrame:CGRectZero];
//        [self addSubview:bkIma];
//        bkIma.image = [UIImage imageNamed:@"01"];
//        bkIma.contentMode = UIViewContentModeScaleAspectFill;
//        bkIma.clipsToBounds = YES;
//        self.bkIma = bkIma;
        
        UILabel *frist = [[UILabel alloc] initWithFrame:CGRectZero];
        frist.textColor = JMBaseColor;
        frist.textAlignment = NSTextAlignmentLeft;
        frist.font = [UIFont systemFontOfSize:18];
        [self addSubview:frist];
        self.frist = frist;
        
        UILabel *sec = [[UILabel alloc] initWithFrame:CGRectZero];
        sec.textAlignment = NSTextAlignmentLeft;
        sec.textColor = JMBaseColor;
        sec.font = [UIFont systemFontOfSize:13];
        [self addSubview:sec];
        self.sec = sec;
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setHTit:(NSArray *)hTit
{
    _hTit = hTit;
    _frist.text = hTit.firstObject;
    _sec.text = hTit.lastObject;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _bkIma.frame = self.bounds;
    _frist.frame = CGRectMake(10, self.height/2-30, self.width-20, 30);
    _sec.frame = CGRectMake(10, CGRectGetMaxY(_frist.frame)+10, self.width-20, 30);
}

@end

#pragma mark - **************** CPSETTableViewCell
@implementation CPSETTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *lIma = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:lIma];
        lIma.contentMode = UIViewContentModeScaleAspectFit;
        lIma.tintColor = JMBaseColor;
        self.lIma = lIma;
        UILabel *tTit = [[UILabel alloc] initWithFrame:CGRectZero];
        tTit.textAlignment = NSTextAlignmentLeft;
        tTit.font = [UIFont systemFontOfSize:DefaultFontSize];
        [self.contentView addSubview:tTit];
        self.tTit = tTit;
    }
    
    return self;
}
- (void)setModel:(CPModel *)model
{
    _model = model;
    _tTit.text = model.title;
    _lIma.image = [UIImage imageNamed:model.icon];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _lIma.frame = CGRectMake(20, self.height/2-11, 22, 22);
    _tTit.frame = CGRectMake(CGRectGetMaxX(_lIma.frame)+20, 5, self.width*0.6, self.height-10);
}
@end

